package com.jyxd.web.service.assessment.basic;

import com.jyxd.web.dao.basic.RecordListDao;
import com.jyxd.web.dao.basic.RecordListItemDao;
import com.jyxd.web.data.basic.RecordList;
import com.jyxd.web.data.basic.RecordListItem;
import com.jyxd.web.data.user.User;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class RecordListItemService {

    @Autowired
    private RecordListItemDao recordListItemDao;
    @Autowired
    private RecordListDao recordListDao;

    /**
     * 保存记录表数据
     * @param jsonObject
     * @param jsonArray
     * @return boolean
     */
    public JSONObject savedate(JSONObject jsonObject, JSONArray jsonArray, User user){
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        json.put("msg", "添加失败");

        String id = jsonObject.getString("id");
        String patientId = jsonObject.getString("patientId");
        String recordType = jsonObject.getString("recordType");
        String operatorCode = jsonObject.getString("operatorCode");
        String status = jsonObject.getString("status");

        if (id.equals("")){//无id 新增记录单及明细
            //新增记录单表数据
            String recordListId = UUIDUtil.getUUID();
            RecordList recordList = new RecordList();
            recordList.setId(recordListId);
            recordList.setPatientId(patientId);
            recordList.setRecordType(recordType);
            recordList.setRecordTime(new Date());
            recordList.setCreateTime(new Date());
            recordList.setOperatorCode(user.getLoginName());
            recordList.setStatus(1);
            boolean insert = recordListDao.insert(recordList);
            //新增记录单明细表数据
            if (insert){
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject obj=(JSONObject) jsonArray.get(i);
                    String itemRecordType = obj.getString("recordType");
                    String itemcode = obj.getString("code");
                    String itemcontent = obj.getString("content");
                    RecordListItem recordListItem = new RecordListItem();
                    recordListItem.setId(UUIDUtil.getUUID());
                    recordListItem.setRecordListId(recordListId);
                    recordListItem.setRecordType(itemRecordType);
                    recordListItem.setCode(itemcode);
                    recordListItem.setContent(itemcontent);
                    recordListItem.setCreateTime(new Date());
                    recordListItem.setOperatorCode(user.getLoginName());
                    recordListItem.setStatus(1);
                    boolean flag = recordListItemDao.insert(recordListItem);
                    if (flag){
                        json.put("msg", "添加成功");
                        json.put("code",HttpCode.OK_CODE.getCode());
                    }
                }
            }
        }else {//有id 修改明细 无需修改记录单表
            //修改记录单表
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id",id);
            RecordList recordList = recordListDao.queryDataById(map);
            recordList.setRecordTime(new Date());
            recordList.setOperatorCode(user.getLoginName());
            recordList.setStatus(Integer.valueOf(jsonObject.getString("status")));
            boolean update = recordListDao.update(recordList);
            //修改记录单明细表
            if (update){
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject obj=(JSONObject) jsonArray.get(i);
                    String itemid = obj.getString("id");
                    Map<String, Object> itemmap = new HashMap<String, Object>();
                    map.put("id",itemid);
                    RecordListItem recordListItem = recordListItemDao.queryDataById(itemmap);
                    String itemcontent = obj.getString("content");
                    recordListItem.setContent(itemcontent);
                    boolean flag = recordListItemDao.update(recordListItem);
                    if (flag){
                        json.put("msg", "修改成功");
                        json.put("code",HttpCode.OK_CODE.getCode());
                    }
                }
            }
        }
        return null;
    }


    /**
     * 更新一条记录明细表数据
     * @param RecordListItem
     * @return boolean
     */
    public boolean update(RecordListItem RecordListItem){
        return recordListItemDao.update(RecordListItem);
    }

    /**
     * 根据条件查询记录明细表数据
     * @param map
     * @return list
     */
    public List<RecordListItem> queryList(Map<String, Object> map){
        return recordListItemDao.queryList(map);
    }

}
